package griffin.website;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import griffin.logic.userLogic;

public class tabframework extends ActivityGroup  {

	private static final String TAG = "[tabframework]";
	public final static String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/sitedata";
	public final static String DATABASE_FILENAME = "web.sqlite";	//database name
	userLogic userlogic = new userLogic(this);

	public static TabHost tab_host;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "[onCreate...] ");	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabframework);

		initDatabase();

		tab_host = (TabHost) findViewById(R.id.websites_tab_host);
		tab_host.setup(this.getLocalActivityManager());

		TabSpec ts1 = tab_host.newTabSpec("TAB_HOME"); 
		ts1.setIndicator("Home"); 
		ts1.setContent(new Intent(this, home.class)); 
		
		tab_host.addTab(ts1); 

//		TabSpec ts2 = tab_host.newTabSpec("TAB_SEARCH"); 
//		ts2.setIndicator("Search"); 
//		ts2.setContent(new Intent(this, search.class)); 
//		tab_host.addTab(ts2); 

		TabSpec ts3 = tab_host.newTabSpec("TAB_BOOKMARK"); 
		ts3.setIndicator("BookMark"); 
		ts3.setContent(new Intent(this, bookmark.class)); 
		tab_host.addTab(ts3); 

		TabSpec ts4 = tab_host.newTabSpec("TAB_MORE"); 
		ts4.setIndicator("More"); 
		ts4.setContent(new Intent(this, More.class)); 
		tab_host.addTab(ts4); 

		tab_host.setCurrentTab(0);


		
	}


	private void initDatabase()
	{

		try
		{
			// get database file's absolute path
			String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;

			//check file
			File dir = new File(databaseFilename);
			//            if(dir.exists())
			//                return;

			// check or create database path
			dir = new File(DATABASE_PATH);

			if (!dir.exists())
				dir.mkdir();

			InputStream input = null;
			OutputStream output = null;
			// read database file flow from resource
			input = getResources().openRawResource(R.raw.web);

			try {
				output = new FileOutputStream(databaseFilename);

				// copy to output stream
				byte[] buffer = new byte[2048];
				int length;
				while ((length = input.read(buffer)) > 0) {
					output.write(buffer, 0, length);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				// close output stream
				try {
					output.flush();
					output.close();
				} catch (IOException e) {
				}
				// close input stream
				try {
					input.close();
				} catch (IOException e) {
				}
			}    
		}
		catch (Exception e)
		{

		}
	}
	
	//
//	private void showTips(){
//		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
//		alertDialog.setTitle("exit app");
//		alertDialog.setMessage("Are you sure to exit ?");
//		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int which) {
//            	userlogic.loginOutWhenExit();
//            	Log.d(TAG,"[Keyboard loginOut true]" );
//           } });
//		alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int which) {
//            	Log.d(TAG,"[Keyboard loginOut false]");
//           } });
//        alertDialog.show();
//	}
//
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
////			userlogic.loginOutWhenExit();
//			Log.d(TAG,"[Keyboard loginOut true]" );
//			showTips();
//			return false;
//		}
//		Log.d(TAG,"[Keyboard loginOut false]");
//		return false;
//	}
	
	@Override
	public void onStop(){
		super.onStop();
//		userlogic.loginOutWhenExit();
		Log.d(TAG,"[onDestroy() loginOut true]" );
		
	}
	
}